﻿using System;
using System.Collections;
using System.Web;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Settings;

namespace ASPNETVN.PORTAL.Components.UI
{
    public class Page : System.Web.UI.Page
    {
        #region Stardand Controls

        /// <summary>
        /// Standard update button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonUpdate;

        /// <summary>
        /// Standard delete button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonDelete;

        /// <summary>
        /// Standard cancel button.
        /// </summary>
        protected System.Web.UI.WebControls.LinkButton ButtonCancel;

        #endregion

        //-----------------------------------------------------------------------------------------
        public void ReturnToReferrerPage()
        {
            // Redirect back to the portal home page
            if (Request.QueryString["ReturnUrl"] != null)
            {
                int pos = Request.RawUrl.IndexOf("returnUrl");
                string returnUrl = Request.RawUrl.Substring(pos + 10);
                Response.Redirect(Server.UrlDecode(returnUrl));
            }
            else
            {
                Response.Redirect((string)ViewState["UrlReferrer"]);
            }
        }

        #region Events

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            this.MasterPageFile = "~/Masters/" + Global.GetMasterFile() + ".Master";
            this.Theme = Global.GetThemeName();
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.ButtonCancel != null)
            {
                this.ButtonCancel.Click += this.ButtonCancel_Click;
                //this.ButtonCancel.Text = Global.RM.GetString("PAGE_BUTTON_GOBACK");
                this.ButtonCancel.CausesValidation = false;
                this.ButtonCancel.EnableViewState = false;
            }

            base.OnInit(e);
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            // Store URL Referrer to return to portal
            if (Request.UrlReferrer != null)
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            else
                ViewState["UrlReferrer"] = "~/Default.aspx";
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Custom events.

        // Cancel event.
        protected virtual void OnCancel(EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        void ButtonCancel_Click(Object sender, EventArgs e)
        {
            this.OnCancel(e);
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Tabs)

        private int _tabID;
        private int _tabIndex;

        public int TabID
        {
            get
            {
                if (this._tabID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabID"] != null)
                        this._tabID = Int32.Parse(Request.Params["TabID"]);
                }
                return this._tabID;
            }
        }

        public int TabIndex
        {
            get
            {
                if (this._tabIndex == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TabIndex"] != null)
                        this._tabIndex = Int32.Parse(Request.Params["TabIndex"]);
                }
                return this._tabIndex;
            }
        }
        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Items)

        private int _itemID;

        public int ItemID
        {
            get
            {
                if (_itemID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ItemID"] != null)
                        _itemID = Int32.Parse(Request.Params["ItemID"]);
                }
                return _itemID;
            }
            set { _itemID = value; }
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Properties (Modules)

        private int _moduleID;

        public int ModuleID
        {
            get
            {
                if (_moduleID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["Mid"] != null)
                        _moduleID = Int32.Parse(Request.Params["Mid"]);
                }
                return _moduleID;
            }
        }

        private Hashtable _settings;

        public Hashtable Settings
        {
            get
            {
                if (this._settings == null)
                {
                    if (ModuleID > 0)
                    {
                        this._settings = PortalConfiguration.GetModuleSettings(ModuleID, this);
                    }
                    else
                    {
                        this._settings = new Hashtable();
                    }
                }

                return this._settings;
            }
        }

        #endregion

        //-----------------------------------------------------------------------------------------

        #region Portal

        public Configuration.Items.Portal Portal
        {
            get
            {
                return (Configuration.Items.Portal)HttpContext.Current.Items["Portal"];
            }
        }

        #endregion
    }
}